<?php
/**
 * Object Relational Persistence Mapping Class for table: rad_ticket
 *
 * @package	com.athena
 * @author	LHV
 */
class RadTicket extends ORDataObject {

	/**#@+
	 * Fields of table: rad_ticket mapped to class members
	 */
	var $rad_ticket_id         		= '';
	var $ticket_id            		= '';
	var $patient_id            		= '';
	var $enum_rad_type	        	= '';
	var $treatment_doctor_id		= '';
	var $treatment_department_id	= '';
	var $request_time          		= '';
	var $enum_patient_type     		= '';
	var $return_time           		= '';
	var $diagnosis             		= '';
	var $enum_ticket_status    		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'rad_ticket';

	/**
	 * Primary Key
	 */
	var $_key = 'rad_ticket_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'RadTicket';

	/**
	 * Handle instantiation
	 */
	function RadTicket() {
		parent::ORDataObject();
	}

	/**
	 * Set data to the object. This is a wrapper of populateArray method
	 *
	 * @param int $ticketId
	 * @param int $patientId
	 * @param int $enumPatientType
	 * @param int $enumRadType
	 * @param int $doctorId
	 * @param int $deptId
	 * @param datetime $requestTime
	 * @param string $diagnosis
	 * @param int $ticketStatus
	 */
	public function setData($ticketId, $patientId, $enumPatientType, $enumRadType, $doctorId,
            $deptId, $requestTime, $diagnosis, $ticketStatus) {
        $data   =   array(
            'ticket_id'                 =>  $ticketId,
            'patient_id'                =>  $patientId,
            'enum_patient_type'         =>  $enumPatientType,
            'enum_rad_type'             =>  $enumRadType,
            'treatment_doctor_id'       =>  $doctorId,
            'treatment_department_id'   =>  $deptId,
            'request_time'              =>  $requestTime,
            'diagnosis'                 =>  $diagnosis,
            'enum_ticket_status'        =>  $ticketStatus
        );
        $this->populateArray($data);
    }

    /**
     * Get all radiology assigment of a medical ticket
     *
     * @param int $ticketId Ticket Id
     * @return array    Result array of assigments
     */
    public function getAssignmentsByTicket($ticketId) {
    	$dateFormat    =   L10nHelper::getSqlDateFormat();
    	$ds    =   & new Datasource_sql();
        $sql   =   array(
            'cols'  =>  "$this->_key, ticket_id, patient_id, enum_rad_type,
                        DATE_FORMAT(request_time, '$dateFormat') request_time , enum_ticket_status",
            'from'  =>  "$this->_table",
            'where' =>  "ticket_id='$ticketId'",
            'orderby'   =>  "request_time"
        );
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }

    /**#@+
     * Getters
     */
    public function getDiagnosis() {
    	return $this->diagnosis;
    }

    public function getTreatmentDoctorId() {
    	return $this->treatment_doctor_id;
    }

    public function getEnumPatientType() {
    	return $this->enum_patient_type;
    }

    public function getTicketId() {
    	return $this->ticket_id;
    }

    public function getEnumTicketStaus() {
    	return $this->enum_ticket_status;
    }

    public function getEnumRadType() {
        return $this->enum_rad_type;
    }

    public function getRequestTime() {
        return L10nHelper::renderDateTime($this->request_time);
    }

    public function getPatientId() {
    	return $this->patient_id;
    }
    /**#@-*/
     /**#@+
     * Setters
     */
    public function setEnumTicketStatus($value) {
        $this->enum_ticket_status   =   $value;
    }
     /**#@-*/
    /**
     * Delete rad_ticket with given id
     *
     * @param int $radTicketId Id of the rad_ticket to be deleted
     */
    public function delete($radTicketId) {
        $sql    =   "DELETE FROM $this->_table WHERE $this->_key=$radTicketId";
        $this->_execute($sql);
    }

    /**
     * Check if a rad_ticket exists or not
     *
     * @param int $radTicketId Id of the rad_ticet to be checked
     * @return bool true if the ticket exists or false else versa
     */
    public function checkTicketExist($radTicketId) {
        $sql    =   "SELECT $this->_key FROM $this->_table WHERE $this->_key=$radTicketId";
        $res    =   $this->_execute($sql);
        if($res && !$res->EOF) {
        	return true;
        }
        return false;
    }

    /**
     * Set status for a radiology ticket
     *
     * @param int $radTicketId Id of the ticket
     * @param unknown_type $enumStatus Status to be set to the ticket
     */
    public function updateTicketStatus($radTicketId, $enumStatus) {
        $sql    =   "UPDATE $this->_table
                    SET enum_ticket_status=$enumStatus
                    WHERE $this->_key='$radTicketId'";
        $this->_execute($sql);
    }

    public function getAssignmentsByPatient($patientId, $enumTicketStatus = false) {
        $dateFormat    =   L10nHelper::getSqlDateFormat();
        $ds    =   & new Datasource_sql();
        $sql   =   array(
            'cols'  =>  "$this->_key, enum_rad_type, DATE_FORMAT(request_time, '$dateFormat') request_time, enum_ticket_status",
            'from'  =>  "$this->_table",
            'where' =>  "patient_id='$patientId'",
            'orderby'   =>  "request_time"
        );

        if ($enumTicketStatus !== false) {
            $sql['where']   .=  " AND enum_ticket_status='$enumTicketStatus'";
        }
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }

    public function hasItemDone($radTicketId) {
        $sql    =   "SELECT rad_ticket_item_id FROM rad_ticket_item WHERE rad_ticket_id='$radTicketId' AND is_done > 0";
        $res    =   $this->_execute($sql);
        if ($res && !$res->EOF) {
        	return true;
        }

        return false;
    }

    public function getPriceInfo($radTicketId) {
    	$priceInfo =   array(
    		'original_price'    =>   0,
            'insurance_price'   =>   0,
            'patient_price'     =>   0
    	);
    	$sql   =   "SELECT rad_ticket_item_id FROM rad_ticket_item WHERE rad_ticket_id='$radTicketId'";
        $res   =   $this->_execute($sql);
        while ($res && !$res->EOF) {
            $objTicketItem  =   Celini::newORDO('RadTicketItem', $res->fields['rad_ticket_item_id']);
            $prices         =   $objTicketItem->getPriceInfo();
            $priceInfo['original_price']    +=  $prices['original_price'];
            $priceInfo['insurance_price']   +=  $prices['insurance_price'];
            $priceInfo['patient_price']     +=  $prices['patient_price'];
            $res->moveNext();
        }

        return $priceInfo;
    }
}
?>
